﻿using DongFangGuoKai.WMSPlatform.Commn.TokenModule;
using DongFangGuoKai.WMSPlatform.Core;
using DongFangGuoKai.WMSPlatform.Domain.Model;
using DongFangGuoKai.WMSPlatform.Service.BasicApp.Cell;
using DongFangGuoKai.WMSPlatform.Service.BasicApp.Cell.DTO;
using DongFangGuoKai.WMSPlatform.Service.Inventory.DTO;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

namespace DongFangGuoKai.WMSPlatform.Service.Inventory
{
    public class InventoryService : IInventoryService
    {
        private readonly SimpleRepositroy<TInventories> inventoryrepost;
        private readonly IConfiguration configuration;
        private readonly SimpleRepositroy<TCells> cellrepost;

        public InventoryService(SimpleRepositroy<TInventories> inventoryrepost, IConfiguration configuration)
        {
            this.inventoryrepost = inventoryrepost;
            this.configuration = configuration;
            cellrepost = inventoryrepost.ChangeRepository<SimpleRepositroy<TCells>>();
        }

        /// <summary>
        /// 物资管理系统同步库存数据
        /// </summary>
        /// <param name="inventoryDTOs"></param>
        /// <returns></returns>
        public bool SynchronousInventory(List<SynchronousInventoryDTO> inventoryDTOs, string? LocationCode, out string Msg)
        {
            Msg = string.Empty;
            try
            {
                //开启事务
                inventoryrepost.AsTenant().BeginTran();
                List<TInventories> inventoryList = new();
                if (LocationCode != null)
                {
                    //获取指定库位的库存数据
                    inventoryList = inventoryrepost.GetList(i => i.LocationCode == LocationCode);
                }
                else
                {
                    inventoryList = inventoryrepost.GetList();
                }

                if (inventoryList.Any())
                {
                    if (!inventoryrepost.Delete(inventoryList))
                    {
                        Msg = "删除原库存数据失败!";
                        inventoryrepost.AsTenant().RollbackTran();
                        return false;
                    }
                }

                List<TInventories> addInventoryList = new();
                foreach (var item in inventoryDTOs)
                {
                    addInventoryList.Add(new()
                    {
                        LocationCode = item.LocationCode,
                        LocationId = item.LocationId,
                        LocationName = item.LocationName,
                        WarehouseId = 70,
                        CreateDate = item.CreateDate,
                        AllocateQty = item.Qty,
                        AvailableQty = item.Qty,
                        PutawayQty = item.Qty,
                        MaterialName = item.MaterialName,
                        MaterialCode = item.MaterialCode,
                        InStockDate = item.CreateDate,
                        //BatchNo = item.BatchNo,
                        //DeptName = item.DeptName,
                        IsLock = false,
                        Status = 1,
                        InStockShortDate = Convert.ToInt32(item.CreateDate.ToString("yyyyMMdd")),
                        StackingStorageId = 0,
                        RelatedOrderDetailId = 0,
                    });
                }
                if (!inventoryrepost.InsertRange(addInventoryList))
                {
                    Msg = "同步库存数据失败!";
                    inventoryrepost.AsTenant().RollbackTran();
                    return false;
                }
                Msg = "库存数据同步成功";
                inventoryrepost.AsTenant().CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                Msg = "同步库存数据失败!" + ex.Message;
                inventoryrepost.AsTenant().RollbackTran();
                return false;
            }
        }

    }
}
 